package com.sugon.api;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.IgnoreAuth;
import com.sugon.entity.*;
import com.sugon.entity.dto.LiveBarPhotoParam;
import com.sugon.service.*;
import com.sugon.util.ApiBaseAction;
import com.sugon.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "工单现场照片API")
@RestController
@RequestMapping("/api/v1/ticketLiveImage")
public class TicketLiveImageApiController extends ApiBaseAction {

    @Resource
    private DevMaintenanceTicketService devPmTicketService;
    @Resource
    private DevAmTicketService devAmTicketService;
    @Resource
    private TicketLiveImageService ticketLiveImageService;
    @Resource
    private SysOssService sysOssService;
    @Resource
    private DevMaintenWorkService repairTicketService;
    @Resource
    private DevRepairRecordService repairRecordService;

    @ApiOperation(value = "获取工单现场照片API")
    @GetMapping("/liveImages")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticketId", value = "维护工单ID", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "ticketType", value = "工单类型:AM(自主维护工单) MAINTENANCE或PM(维护工单) REPAIR(维修工单)", paramType = "query", dataType = "string", allowableValues = "MAINTENANCE,REPAIR", required = true),
            @ApiImplicitParam(name = "groupName", value = "分组名称", paramType = "query", dataType = "string"),
    })
    @ResponseBody
    public ApiResult<List<String>> loadLiveImages(@RequestParam String ticketId, @RequestParam String ticketType, String groupName) {
        ApiResult<List<String>> apiResult = new ApiResult<>();
        return apiResult.success(ticketLiveImageService.getTicketLiveImages(groupName, ticketId, ticketType));
    }

    @IgnoreAuth
    @ApiOperation(value = "上传照片API")
    @PostMapping("/uploadPhoto")
    public ApiResult<String> uploadLiveBarPhoto(@RequestParam("file") MultipartFile file, @RequestParam("module") String module, @RequestParam("subModule") String subModule) throws Exception {
        ApiResult<String> apiResult = new ApiResult<>();
        // 上传图片到文件服务器获取存储路径等相关信息
        R uploadResult = sysOssService.upload(module, subModule, file);
        return apiResult.succMsg(MapUtil.getStr(uploadResult, "url"));
    }

    @IgnoreAuth
    @ApiOperation(value = "管理现场照片API")
    @PostMapping("/manageLiveBarPhoto")
    public ApiResult<String> manageLiveBarPhoto(@RequestBody @Validated LiveBarPhotoParam liveBarPhotoParam, BindingResult bindingResult) throws Exception {
        // 常规校验
        ApiResult<String> apiResult = getValidApiResult(bindingResult);
        if (apiResult != null) {
            return apiResult;
        }
        apiResult = new ApiResult<>();
        String ticketType = liveBarPhotoParam.getTicketType();
        if (StrUtil.equalsAny(ticketType, "MAINTENANCE", "PM")) {
            // 校验设备是否存在
            DevMaintenanceTicketEntity ticket = devPmTicketService.queryObject(liveBarPhotoParam.getTicketId());
            if (ticket == null) {
                return apiResult.failMsg("设备维护工单不存在！");
            }
        } else if ("REPAIR".equals(ticketType)) {
            // 校验设备是否存在
            DevRepairRecordEntity repairTicket = repairRecordService.queryObject(liveBarPhotoParam.getTicketId());
            if (repairTicket == null) {
                return apiResult.failMsg("设备维修工单不存在！");
            }
        } else if ("AM".equals(ticketType)) {
            // 校验设备是否存在
            DevAmTicketEntity devAmTicketEntity = devAmTicketService.queryObject(liveBarPhotoParam.getTicketId());
            if (devAmTicketEntity == null) {
                return apiResult.failMsg("设备自主维护工单不存在！");
            }
        }
        return ticketLiveImageService.batchUpdateTicketImages(liveBarPhotoParam.getTicketId(), ticketType, liveBarPhotoParam.getGroupName(), liveBarPhotoParam.getPaths()) ? apiResult.succMsg("上传成功") : apiResult.failMsg("");
    }

}
